package org.bonorsoft.modules.mobile.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.constant.CacheConstant;
import org.bonorsoft.common.constant.UserConstants;
import org.bonorsoft.common.util.StringUtils;
import org.bonorsoft.modules.mobile.entity.MobileDict;
import org.bonorsoft.modules.mobile.mapper.MobileDictMapper;
import org.bonorsoft.modules.mobile.service.IMobileDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class IMobileDictServiceImpl extends ServiceImpl<MobileDictMapper, MobileDict> implements IMobileDictService {
    @Autowired
    private MobileDictMapper mobileDictMapper;


    /**
     * 通过查询指定code 获取字典值text
     *
     * @param code
     * @param key
     * @return
     */

    @Override
    @Cacheable(value = CacheConstant.SYS_DICT_CACHE, key = "#code+':'+#key")
    public String queryDictTextByKey(String code, String key) {
        log.info("无缓存dictText的时候调用这里！");
        return mobileDictMapper.queryDictTextByKey(code, key);
    }


    /**
     * 通过查询指定table的 text code 获取字典值text
     * dictTableCache采用redis缓存有效期10分钟
     *
     * @param table
     * @param text
     * @param code
     * @param key
     * @return
     */
    @Override
    @Cacheable(value = CacheConstant.SYS_DICT_TABLE_CACHE)
    public String queryTableDictTextByKey(String table, String text, String code, String key) {
        log.info("无缓存dictTable的时候调用这里！");
        return mobileDictMapper.queryTableDictTextByKey(table, text, code, key);
    }

    /**
     * 校验字典类型称是否唯一
     *
     * @param dict 字典类型
     * @return 结果
     */
    @Override
    public String checkDictTypeUnique(MobileDict dict) {
        String dictId = StringUtils.isNull(dict.getId()) ? "-1" : dict.getId();
        QueryWrapper<MobileDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_code", dict.getDictCode());
        MobileDict dictType = this.getOne(queryWrapper);
        if (StringUtils.isNotNull(dictType) && !dictType.getId().equals(dictId)) {
            return UserConstants.DICT_TYPE_NOT_UNIQUE;
        }
        return UserConstants.DICT_TYPE_UNIQUE;
    }

    @Override
    public String getDictCode(String dictId) {
        QueryWrapper<MobileDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", dictId);
        return this.getOne(queryWrapper).getDictCode();
    }

    /**
     * 清空缓存数据
     */
    @Override
    @Caching(evict = {@CacheEvict(value = CacheConstant.SYS_DICT_CACHE_TOTAL, allEntries = true),
            @CacheEvict(value = CacheConstant.SYS_DICT_CACHE_ITME, allEntries = true),
            @CacheEvict(value = CacheConstant.SYS_DICT_CACHE, allEntries = true),
            @CacheEvict(value = CacheConstant.SYS_DICT_TABLE_CACHE, allEntries = true)})
    public void clearCache() {
    }

    /**
     * 获取永安字典值
     *
     * @param table
     * @param text
     * @param code
     * @param key
     * @param param
     * @return
     */
    @Override
    @Cacheable(value = CacheConstant.SYS_YONG_TABLE_CACHE)
    public String queryValueByParam(String table, String text, String code, String key, String param) {
        log.info("永安系统-无缓存dictTable的时候调用这里！");
        return mobileDictMapper.queryValueByParam(table, text, code, key, param);
    }

}
